package cn.imvc.app.controllers.goods;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.entities.Country;
import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.GoodsCountry;
import cn.imvc.app.models.CountryModel;
import cn.imvc.app.models.GoodsCountryModel;
import cn.imvc.app.models.GoodsModel;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

public class GoodsCountryControllerImpl implements GoodsCountryController{
	static Logger logger = Logger.getLogger(GoodsCountryControllerImpl.class);
	public Page<GoodsCountry> page;
	public ViewRequest viewRequest;
	public GoodsCountry goodsCountry;
	public int id;
	public Goods goods;
	public Country countryId_g;
	public Country countryId_h;
	@Autowired
	GoodsCountryModel goodsCountryModel;
	@Autowired
	GoodsModel goodsModel;
	@Autowired
	CountryModel countryModel;
	@Autowired
	LoggingModel loggingModel;
	public String search() {
		return null;
	}

	public String index() {
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		page = goodsCountryModel.find(viewRequest);
		return "goods-country/index";
	}

	public String show() {
		return null;
	}

	public String editNew() {
		return "goods-country/edit";
	}

	public String create() {
		goodsCountry.setGoods(goodsModel.get(goods.getId()));
		goodsCountry.setCountryId_g(countryModel.get(countryId_g.getId()));
		goodsCountry.setCountryId_h(countryModel.get(countryId_h.getId()));
		int result = goodsCountryModel.save(goodsCountry);
		if(result==0){
			return Render.json(new DialogBean("300","商品名称不能重复","","closeCurrent"));
		}
		loggingModel.info(logger,"添加原产国："+goodsCountry.getGoods());
		return Render.json(new DialogBean("200","添加成功","","closeCurrent"));
	}

	public String edit() {
		goodsCountry = goodsCountryModel.get(id);
	    goods = goodsCountry.getGoods();
		countryId_g = goodsCountry.getCountryId_g();
		countryId_h = goodsCountry.getCountryId_h();
		return "goods-country/edit";
	}

	public String update() {
		goodsCountry.setGoods(goodsModel.get(goods.getId()));
		goodsCountry.setCountryId_g(countryModel.get(countryId_g.getId()));
		goodsCountry.setCountryId_h(countryModel.get(countryId_h.getId()));
		int result = goodsCountryModel.update(id,goodsCountry);
		if(result==0){
			return Render.json(new DialogBean("300","商品名称不能重复","","closeCurrent"));
		}
		loggingModel.info(logger,"修改原产国："+goodsCountry.getGoods());
		return Render.json(new DialogBean("200","修改成功","","closeCurrent"));
	}

	public String destroy() {
		goodsCountryModel.delete(id);
		loggingModel.info(logger,"删除原产国："+id);
		return Render.json(new DialogBean("200","删除成功",""));
	}

	public String destroyBatch() {
		return null;
	}

	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public GoodsCountry getGoodsCountry() {
		return goodsCountry;
	}

	public void setGoodsCountry(GoodsCountry goodsCountry) {
		this.goodsCountry = goodsCountry;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public Country getCountryId_g() {
		return countryId_g;
	}

	public void setCountryId_g(Country countryId_g) {
		this.countryId_g = countryId_g;
	}

	public Country getCountryId_h() {
		return countryId_h;
	}

	public void setCountryId_h(Country countryId_h) {
		this.countryId_h = countryId_h;
	}


}
